/*
 * This file is part of GuitarMaster.
 * 
 * GuitarMaster is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GuitarMaster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.paris5.master2.guitarmaster.service;

/*
 * $Id: ServiceCustomer.java 117 2013-04-09 16:28:33Z nedjahi.mourad@gmail.com $
 * 
 * $HeadURL: http://com-paris5-master2-guitarmaster-ejb.googlecode.com/svn/trunk/com.paris5.master2.guitarmaster-ejb/src/java/com/paris5/master2/guitarmaster/service/ServiceCustomer.java $
 * $Revision: 117 $
 * $Date: 2013-04-09 16:28:33 +0000 (Tue, 09 Apr 2013) $
 * $Author: nedjahi.mourad@gmail.com $
 */
import com.paris5.master2.guitarmaster.bean.EcomScore;
import com.paris5.master2.guitarmaster.bean.EcomUser;
import com.paris5.master2.guitarmaster.dao.EcomDao;
import com.paris5.master2.guitarmaster.entity.Score;
import com.paris5.master2.guitarmaster.entity.User;
import com.paris5.master2.guitarmaster.exception.EcomQueryException;
import com.paris5.master2.guitarmaster.exception.EcomException;
import com.paris5.master2.guitarmaster.exception.EcomValidationException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 * Customer interface, list alla user actions
 *
 * @author Leticia Mahamed, Alice Khammy, mourad nedjahi, Saliou Mamadou Barry
 */
@Stateless(mappedName="customer")
public class ServiceCustomer implements ServiceCustomerRemote {
    
    @EJB private EcomDao dao; 
    
    @Override
    public EcomUser authentificate(String login, String pwd) throws EcomValidationException, EcomQueryException {
        return authent(login, pwd, 1);
    }

    @Override
    public EcomUser adminAuthentificate(String login, String pwd) throws EcomValidationException, EcomQueryException {
        return authent(login, pwd, 2);
    }
    
    private EcomUser authent(String login, String pwd, int status) throws EcomValidationException, EcomQueryException {
        
        if(login == null || "".equals(login) || pwd == null || "".equals(pwd)){
            throw new EcomValidationException("Invalid Login / password" );
        }
        
        List<Object> params = new ArrayList<Object>();
        params.add(login);
        params.add(pwd);
        params.add(status);
        
        User result = (User) dao.execute("User.authentificate", params);
        if(result!=null){
            return result.getEcomUser();
        }
        
        return null;
    } 
    
    @Override
    public void subscribe(EcomUser bean) throws EcomValidationException, EcomException, EcomQueryException {
        
        if(bean==null){
            throw new EcomValidationException("Empty user sent ! (EcomUser == null)");
        }
        
        User u = new User(bean);
        u.setStatus(1);
        List<Object> params = new ArrayList<Object>();
        params.add(bean.getEmail());
        Object result = dao.execute("User.findByEmail", params);
        
        if(result!=null){
            throw new EcomException(bean,"User already exists !");
        }
        
        dao.insert(u.getScore());
        dao.insert(u);
    }
    
    @Override
    public void unsubscribe(EcomUser bean) throws EcomValidationException, EcomException, EcomQueryException {
        
        if(bean==null){
            throw new EcomValidationException("Empty user sent ! (EcomUser == null)");
        }
        
        List<Object> params = new ArrayList<Object>();
        params.add(bean.getEmail());
        Object result = dao.execute("User.findByEmail", params);
        
        dao.delete(result);
        
        result = dao.execute("User.findByEmail", params);
        if(result!=null){
            throw new EcomException(bean, "remove error !");
        }
    }
    
    @Override
    public void setUser(EcomUser bean) throws EcomQueryException, EcomValidationException {
        
        if(bean==null){
            throw new EcomValidationException("Empty user sent ! (EcomUser == null)");
        }
        
        User u = new User(bean);
        dao.update(bean);
    }

    @Override
    public EcomUser getUser(int id) throws EcomQueryException, EcomException {

        User u = (User) dao.find(User.class, id);
        if(u!=null){
            return u.getEcomUser();  
        }
        
        return null;
    }

    @Override
    public EcomScore setUserScore(int id_user, boolean increase) throws EcomValidationException {
        
        User u = (User) dao.find(User.class, id_user);
        
        if (u != null) {
            Score score = u.getScore();
            int vote = score.getVoteCount();
            int value = score.getVoteValue();
            vote++;
            value = increase? ++value : 0;
            score.setVoteCount(vote);
            score.setVoteValue(value);
            u.setScore(score);
            dao.update(u);
            return score.getEcomScore();
        }
        else{
            throw new EcomValidationException("User doesn't exists");
        }
    }

    @Override
    public void setAmount(int id, float amount) throws EcomValidationException, EcomException {
        
        if(id==0 || amount <=0){
            throw new EcomValidationException("wrong parameters id="+id+" ammount="+amount);
        }

        User u = (User) dao.find(User.class, id);
        if(u!=null){
            u.setBalance(u.getBalance()+amount);
            dao.update(u);
        }
        else{
            throw new EcomException(null, "User doesn't exists");
        }
    }

    @Override
    public void withdrawUserBalance(Integer id_user, Float amount) throws EcomValidationException {
        User user = (User) dao.find(User.class, id_user);
        if (user == null) {
            throw new EcomValidationException("This user does not exist in data base");
        }
        
        Float oldBalance = user.getBalance();
        Float newBalance = oldBalance - amount;
        if (newBalance < 0) {
            throw new EcomValidationException("Requested amount exceeds the balance! impossible operation");
        }
        user.setBalance(newBalance);
        dao.update(user);
        
    }
}